#!/usr/bin/bash

if [ "$#" -ne 2 ]; then
    echo "Usage: 
    使用pyscenic进行转录因子识别分析，需要两个参数，第一个表达谱csv文件，要求基因在列样本在行，第二个od目录。结果生成一个pyscenic_output.rds文件，里面是一个list，包含regulons以及regulonsAUC。
    示例：
    $0 <loom_file> <od>"
    exit 1
fi

# The name of the file that contains the expression matrix for the single cell experiment. Two file formats are supported: csv (rows=cells x columns=genes) or loom (rows=genes x columns=cells)

#seurat2loom_exprMat.r
# /Pub/Users/wangyk/Project_wangyk/Codelib_YK/some_scr/SC/pysceinc/seurat2loom_exprMat.r $1 $2

# 运行pySCENIC
source /Pub/Users/wangyk/miniconda3/bin/activate pyscenic


# dir=/Pub/Users/wangyk/project/Poroject/F240108002_SKCM_sc/out/4.sc_houxu/十.3/sceinc_put/ #改成自己的目录
tfs=/Pub/Users/wangyk/project/Poroject/F240108002_SKCM_sc/out/4.sc_houxu/十.3/scenic_data/hs_hgnc_tfs.txt

feather=/Pub/Users/wangyk/project/Poroject/F240108002_SKCM_sc/out/4.sc_houxu/十.3/scenic_data/hg19-tss-centered-10kb-10species.mc9nr.genes_vs_motifs.rankings.feather

tbl=/Pub/Users/wangyk/project/Poroject/F240108002_SKCM_sc/out/4.sc_houxu/十.3/scenic_data/motifs-v9-nr.hgnc-m0.001-o0.0.tbl

od=$2
input_csv=$1
date_now=`date`

echo -e "tfs: $tfs \nfeather: $feather\n tbl: $tbl\n"

echo -e ">> 2.1 Starting grn...  [$date_now]\n"

pyscenic grn \
--num_workers 36 \
--output $od/adj.sample.tsv \
--method grnboost2 \
$input_csv \
$tfs 

echo -e "\n\n"


#2.2 cistarget
echo -e ">> 2.2 cistarget...  [$date_now]\n"

pyscenic ctx \
$od/adj.sample.tsv $feather \
--annotations_fname $tbl \
--expression_mtx_fname $input_csv  \
--mode "dask_multiprocessing" \
--output $od/reg.csv \
--num_workers 20  \
--mask_dropouts

echo -e "\n\n"


#2.3 AUCell
echo -e ">> 2.3 AUCell...  [$date_now]\n"

pyscenic aucell $input_csv $od/reg.csv \
--output $od/out_SCENIC.csv \
--num_workers 36 \
--transpose 

echo -e "\n\n"


echo -e ">> 2.4 Convert pvSCENIC result loom file to rds file ... [$date_now]\n"
/Pub/Users/wangyk/Project_wangyk/Codelib_YK/some_scr/SC/pysceinc/sceinc_res_loom2rds.r $od/out_SCENIC.loom


echo -e ">> 2.5 pyscenic over  [$date_now]\n"